/**
 * Created by pankaj on 25/10/14.
 * It accepts files and then make html to render it.
 */
define(['jquery', 'mustache', 'module/template/homeTemplate'], function ($, Mustache, template) {

    var imageRender = function (files) {
        if( !(files instanceof Array)){
            files = [files];
        }
        var fileArea = $(template['home-update-show-image']);

        var fileItem = function (file) {
            var imageItem;
            if ((/^image*/g).test(file.type)) {
                imageItem = $(Mustache.render(template['home-update-image-item'], {id: file.id}));
            } else {
                imageItem = $(Mustache.render(template['home-update-file-item'], {name: file.name, id: file.id}));
            }
            return imageItem;
        };

        $.each(files, function (index, file) {
            var imageHtml = fileItem(file);
            fileArea.append(imageHtml);
            // remove This file item.
            imageHtml.find('a:last').click(function () {
                // remove the model.
                files.splice(index, 1);
                // remove the view.
                imageHtml.remove();
            });
        });
        return fileArea;
    };

    return imageRender;

})
;